﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;

namespace DigitsRecognizer
{
    class LogOnHandler
    {
        private Dictionary<string, string> Users;
        List<object> Result;
        string UsersData;

        public LogOnHandler() 
        {
            Users = new Dictionary<string, string>();
            Result = new List<object>();
            LoadUsers();
        }

        private void LoadUsers() 
        {
            try
            {
                FileStream fs = new FileStream("Users.txt", FileMode.OpenOrCreate, FileAccess.Read);
                StreamReader sr = new StreamReader(fs);
                UsersData = sr.ReadToEnd();
                sr.Close();
                ConvertToDictionary();
            }
            catch (IOException ex)
            {
                Console.WriteLine(ex);
            }
        }

        private void ConvertToDictionary() 
        { 
            int indexStart, indexEnd;
            string temp, key, value;

            UsersData = UsersData.Replace("\n", " ");
            UsersData = UsersData.Replace("\t", " ");
            UsersData = UsersData.Replace("\r", " ");

            while (UsersData.Length > 0)
            {
                indexStart = UsersData.IndexOf("{L:") + 3;
                indexEnd = UsersData.IndexOf("P:");
                if (indexEnd == -1)
                {
                    break;
                }
                temp = UsersData.Substring(indexStart, indexEnd - indexStart);
                key = temp.Trim();
                UsersData = UsersData.Substring(indexEnd);
                indexStart = UsersData.IndexOf("P:") + 3;
                indexEnd = UsersData.IndexOf("}");
                temp = UsersData.Substring(indexStart, indexEnd - indexStart);
                UsersData = UsersData.Substring(indexEnd + 1);
                value = temp;
                Users.Add(key, value);
            }
        }

        public List<object> CreateAccount(string login, string password) 
        {
            Result = new List<object>();
            if (Users.ContainsKey(login.ToLower()))
            {
                Result.Add("Podany login istnieje już w bazie! Wprowadz nowy login i hasło.");
                Result.Add("Utworzenie konta użytkownika");
                Result.Add(MessageBoxButtons.OK);
                Result.Add(MessageBoxIcon.Error);
                return Result;
            }
            else 
            {
                SaveData(login, password);
                Result.Add(string.Format("Dodano użytkownika o loginie: {0}", login));
                Result.Add("Utworzenie konta użytkownika");
                Result.Add(MessageBoxButtons.OK);
                Result.Add(MessageBoxIcon.Information);
                return Result;
            }
        }

        private void SaveData(string login, string password) 
        {
            ConvertToSave(login, password);
            try
            {
                FileStream fs = new FileStream("Users.txt", FileMode.Append, FileAccess.Write);
                StreamWriter sw = new StreamWriter(fs);
                sw.Write(UsersData);
                sw.Close();
            }
            catch (IOException ex)
            {
                Console.WriteLine(ex);
            }
        }

        private void ConvertToSave(string login, string password) 
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("{L: ");
            sb.Append(login.ToLower());
            sb.Append(" P: ");
            sb.Append(password.GetHashCode());
            sb.Append("}");
            sb.AppendLine();
            UsersData = sb.ToString();
        }

        public List<object> CheckUserInDB(string login, string password) 
        {
            Result = new List<object>();
            if (Users.ContainsKey(login.ToLower()))
            {
                string pass = password.GetHashCode().ToString();
                if (!Users[login.ToLower()].Equals(pass))
                {
                    Result.Add(string.Format("Hasło dla {0} jest niepoprawne. Wprowadz poprawne hasło.", login));
                    Result.Add("Logowanie");
                    Result.Add(MessageBoxButtons.OK);
                    Result.Add(MessageBoxIcon.Error);
                }
                
            }
            else
            {
                Result.Add(string.Format("Login {0} nie istnieje w bazie! Wprowadz poprawny login i hasło.", login));
                Result.Add("Logowanie");
                Result.Add(MessageBoxButtons.OK);
                Result.Add(MessageBoxIcon.Error);
            }
            return Result;
        }

        // messages
        //private object[] Create
    }
}
